package com.ptu.servlet;

import com.google.gson.Gson;
import com.ptu.entity.Position;
import com.ptu.service.PositionService;
import com.ptu.service.impl.PositionServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 *
 */
@WebServlet("/ptu/PositionServlet")
public class PositionServlet extends BaseServlet {

    private PositionService posService = new PositionServiceImpl();

    // 使用 ajax 查询岗位信息
    private void positionFindAll1(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<Position> posList = posService.selectAll();
        String posJson = new Gson().toJson(posList);
        resp.getWriter().print(posJson);
    }

    // 删除指定岗位信息
    private void positionRemove(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        Integer posid = toInteger(req.getParameter("posid"));
        int del = posService.delPos(posid);

        if(del > 0){
            resp.sendRedirect(req.getContextPath() + "/ptu/PositionServlet?method=positionFindAll");
        }else {
            req.setAttribute("msg", "修改失败");
            req.getRequestDispatcher("/positionList.jsp").forward(req, resp);
        }

    }

    // 修改//指定岗位信息
    private void positionupdate(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

        // 1. 从前端获取数据
        Position pos = createPos(req, resp);

        // 2. 对数据进行操作
        int change = posService.positionupdate(pos);

        // 3. 给用户进行响应
        if (change > 0) {
            resp.sendRedirect(req.getContextPath() + "/ptu/PositionServlet?method=positionFindAll");
        } else {
            req.setAttribute("msg", "修改失败");
            req.getRequestDispatcher("/positionUpdate.jsp").forward(req, resp);
        }

    }


    // 查询单个岗位信息
    private void positionFindOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 获取前端数据
        String posId = req.getParameter("posid");

        // 2. 数据处理
        Position pos = posService.findPosByposid(posId);

        // 3. 返回结果
        if (pos != null) {
            req.setAttribute("pos", pos);
            req.getRequestDispatcher("/positionUpdate.jsp").forward(req, resp);
        }

    }

    // 查询岗位信息
    private void positionFindAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Position> pos = posService.selectAll();

        req.setAttribute("pos",pos);
        req.getRequestDispatcher("/positionList.jsp").forward(req,resp);
    }

    // 添加岗位操作
    private void positionSave(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        //1.接受页面的数据
        Position pos = createPos(req, resp);

        //2.数据的处理
        int save = posService.insert(pos);

        //3.给用户做出响应
        if (save > 0){
            resp.sendRedirect(req.getContextPath() + "/ptu/PositionServlet?method=positionFindAll");
        }else {
            req.setAttribute("msg","添加失败");
            req.getRequestDispatcher("/position.html").forward(req,resp);
        }

    }

    // 创建 Position 对象
    private Position createPos(HttpServletRequest req, HttpServletResponse resp) {

        Integer posid = toInteger(req.getParameter("posid"));
        String pname = req.getParameter("pname");
        String pdesc = req.getParameter("pdesc");

        Position pos = new Position(posid,pname,pdesc);

        return pos;
    }


    private Integer toInteger(String value) {
        return value == null ? null : Integer.parseInt(value);
    }

}
